<%@ page import="com.minxi.jsp.utils.DruidUtil" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.Date" %>
<%@ page import="com.minxi.jsp.javabean.User" %>
<%--
  Created by IntelliJ IDEA.
  User: 23738
  Date: 2024/6/6
  Time: 18:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
<link rel="stylesheet" type="text/css" href="../../css/2024-06-06_index2.css">
<body>
<%!
    public static ArrayList<User> users2;

    public static void jdbc() {
        // 定义null值
        PreparedStatement st = null;
        Connection conn = null;
        ResultSet rs = null;
        users2 = new ArrayList<>();
        try {

            conn = DruidUtil.DruidConnect();
            // 4.定义sql（预编译）
            String sql = "select * from user_info;";

            // 5.获取sql对象
            st = conn.prepareStatement(sql);

            // 6.执行sql语句
            rs = st.executeQuery();
            while (rs.next()) {
                // 把从数据库拿到的数据存起来
                int id = rs.getInt("id");
                String userName = rs.getString("user_name");
                String passWord = rs.getString("user_pwd");
                int age = rs.getInt("user_age");
                int sex = rs.getInt("user_sex");
                String userPhone = rs.getString("user_phone");
                String userAddress = rs.getString("user_address");
                Date userTime = rs.getDate("user_time");
                int roleType = rs.getInt("role_type");

                // 用拿到的数据创建用户对象
                User user = new User();
                user.setId(id);
                user.setUserName(userName);
                user.setPassWord(passWord);
                user.setAge(age);
                user.setSex(sex);
                user.setUserPhone(userPhone);
                user.setUserAddress(userAddress);
                user.setUserTime(userTime);
                user.setRoleType(roleType);

                // 往用户列表里添加用户对象
                users2.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                DruidUtil.DruidClose(rs, st, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
%>
<div class="top">
    <div class="box_1">
        <form action="query.jsp" method="post">
            <input placeholder="用户名" name="username" required>
            <input placeholder="年龄" name="age" required oninput="this.value = this.value.replace(/[^0-9.]/g, '')">
            <input type="submit" value="查询" id="submit">
            <a href="new.jsp">新增</a>
        </form>
    </div>
    <div class="box_2">
        <%!
            public String username(Integer id) {

                PreparedStatement st = null;
                Connection conn = null;
                ResultSet rs = null;
                String name = "xxx";
                try {

                    conn = DruidUtil.DruidConnect();
                    // 4.定义sql（预编译）
                    String sql = "select user_name from user_info where id = ?;";
                    st = conn.prepareStatement(sql);
                    // 5.获取sql对象
                    st.setInt(1, id);
                    rs = st.executeQuery();

                    // 6.执行sql语句
                    while (rs.next()) {
                        name = rs.getString("user_name");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        DruidUtil.DruidClose(rs, st, conn);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return name;
            }
        %>
        <%
            request.setCharacterEncoding("UTF-8");
            String id = request.getParameter("id");
            if (null == id){
                id = "0";
            }
            String username= username(Integer.valueOf(id));
        %>
        <p class="welcome"><%=username%>，登录成功</p>
    </div>
</div>

<div class="select">
    <div class="nev">
        <table class="jdbc">
            <tr>
                <th>用户编号</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>性别</th>
                <th>号码</th>
                <th>地址</th>
                <th>时间</th>
                <th>职业</th>
                <th>操作</th>
            </tr>
            <%
                jdbc();
                // 判断学生列表是否为空并打印
                if (!users2.isEmpty()) {
                    for (User user : users2) {
            %>

            <!-- 遍历的代码内容-->
            <tr>
                <td><%=user.getId()%>
                </td>
                <td><%=user.getUserName()%>
                </td>
                <td><%=user.getAge()%>
                </td>
                <td><%=user.getSex() == 1 ? "男" : "女"%>
                </td>
                <td><%=user.getUserPhone()%>
                </td>
                <td><%=user.getUserAddress()%>
                </td>
                <td><%=user.getUserTime()%>
                </td>
                <td><%=user.getRoleType() == 1 ? "老师" : "学生"%>
                </td>
                <td class="action">
                    <a href="detail.jsp?id=<%=user.getId()%>" id="show">查看详情</a>
                    <a href="update.jsp?id=<%=user.getId()%>" id="update">编辑</a>
                    <a href="delete.jsp?id=<%=user.getId()%>&username=<%=user.getUserName()%>" id="delete">删除</a>
                </td>
            </tr>

            <%}%>
            <%
                } else {
                    out.print("<tr align=\"center\">" +
                            "<td rowspan=8>抱歉，没有查询到符合条件的学生~</td>" +
                            "<td></td>" +
                            "<td></td>" +
                            "<td></td>" +
                            "<td></td>" +
                            "<td></td>" +
                            "<td></td>" +
                            "<td></td>" +
                            "</tr>");
                }
            %>
        </table>
    </div>
</div>
</body>
</html>
